package com.cx.controller;

import com.cx.common.ApiReturn;
import com.cx.pojo.Discount;
import com.cx.service.DiscountService;
import org.omg.CORBA.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.print.DocFlavor;
import java.util.List;

@Controller
@RequestMapping("discount")
@CrossOrigin//跨域
public class DiscountController {
    @Autowired
    DiscountService discountService;

    @RequestMapping("insertDiscount")
    @ResponseBody
public ApiReturn insertDiscount(Integer carId, Integer discountMoney, String discountShop){
        Discount discount = new Discount();
        discount.setCarId(carId);
        discount.setDiscountMoney(discountMoney);
        discount.setDiscountShop(discountShop);
        discount.setStatus(1);
        int rows = discountService.insert(discount);
        if (rows > 0){
            return ApiReturn.success("插入成功");
        }else {
            return ApiReturn.error("插入失败");
        }
    }

    @RequestMapping("updateById")
    @ResponseBody
    public ApiReturn updateById(Integer discountId,Integer carId,Integer discountMoney,String discountShop){
        Discount discount = new Discount();
        discount.setCarId(carId);
        discount.setDiscountMoney(discountMoney);
        discount.setDiscountShop(discountShop);
        discount.setStatus(1);
        int rows = discountService.updateById(discountId,discount);
        if (rows > 0){
            return ApiReturn.success("更新成功");
        }else {
            return ApiReturn.error("更新失败");
        }
    }

    @RequestMapping("deleteById")
    @ResponseBody
public ApiReturn deleteById(Integer discountId){
        int rows = discountService.deleteById(discountId);
        if (rows > 0){
            return ApiReturn.success("删除成功");
        }else {
            return ApiReturn.error("删除失败");
        }
    }

    @RequestMapping("selectByCarId")
    @ResponseBody
    public ApiReturn selectByCarId(Integer carId){
        List<Discount> discounts = discountService.selectByCarId(carId);
        if (discounts != null){
            return ApiReturn.success("按Id查找结果",discounts);
        }else {
            return ApiReturn.error("查询失败");
        }
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public ApiReturn selectAll(){
        List<Discount> discounts = discountService.selectAll();
        if (discounts != null){
            return ApiReturn.success("查找结果",discounts);
        }else {
            return ApiReturn.error("查询失败");
        }
    }

}
